package com.piper.long_connection.pb_server;

import com.google.protobuf.GeneratedMessage;
import com.piper.long_connection.pb_model.core.model.Request;
import com.piper.long_connection.pb_model.core.model.Response;
import com.piper.long_connection.pb_model.core.model.Result;
import com.piper.long_connection.pb_model.core.model.ResultCode;
import com.piper.long_connection.pb_server.scanner.Invoker;
import com.piper.long_connection.pb_server.scanner.InvokerHoler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;


/**
 * 消息接受处理类
 *
 * @author -琴兽-
 */
public class ServerHandler extends SimpleChannelInboundHandler<Request> {

    /**
     * 接收消息
     */
    @Override
    public void messageReceived(ChannelHandlerContext ctx, Request request) {
        handlerMessage(ctx.channel(), request);
    }


    /**
     * 消息处理
     *
     * @param channel
     * @param request
     */
    private void handlerMessage(Channel channel, Request request) {
        Response response = new Response(request);
        System.out.println("module:" + request.getModule() + "   " + "cmd：" + request.getCmd());

        //获取命令执行器
        Invoker invoker = InvokerHoler.getInvoker(request.getModule(), request.getCmd());
        if (invoker != null) {
            try {
                Result<?> result = (Result<?>) invoker.invoke(request.getData());

                //判断请求是否成功
                if (result.getResultCode() == ResultCode.SUCCESS) {
                    //回写数据
                    Object object = result.getContent();
                    if (object != null) {
                        if (object instanceof GeneratedMessage) {
                            GeneratedMessage content = (GeneratedMessage) object;
                            response.setData(content.toByteArray());
                        } else {
                            System.out.println(String.format("不可识别传输对象:%s", object));
                        }
                    }
                    channel.write(response);
                } else {
                    //返回错误码
                    response.setStateCode(result.getResultCode());
                    channel.write(response);
                    return;
                }
            } catch (Exception e) {
                e.printStackTrace();
                //系统未知异常
                response.setStateCode(ResultCode.UNKOWN_EXCEPTION);
                channel.write(response);
            }
        } else {
            //未找到执行者
            response.setStateCode(ResultCode.NO_INVOKER);
            channel.write(response);
            return;
        }
    }

    /**
     * 新客户端接入
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channelActive");
    }

    /**
     * 断线移除会话
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channelInactive");
    }

}
